using Microsoft.Phone.Controls.Maps.Core;
using Microsoft.Phone.Controls.Maps.Design;
using System;
using System.ComponentModel;
using System.Device.Location;
using System.Windows;
namespace Microsoft.Phone.Controls.Maps
{
	public class LocationRectTileSource : TileSource
	{
		[TypeConverter(typeof(LocationRectConverter))]
		public LocationRect BoundingRectangle
		{
			get;
			set;
		}
		[TypeConverter(typeof(RangeConverter))]
		public Range<double> ZoomRange
		{
			get;
			set;
		}
		public LocationRectTileSource()
		{
		}
		public LocationRectTileSource(string uriFormat, LocationRect boundingRectangle, Range<double> zoomRange) : base(uriFormat)
		{
			this.BoundingRectangle = boundingRectangle;
			this.ZoomRange = zoomRange;
		}
		public override Uri GetUri(int x, int y, int zoomLevel)
		{
			Uri result = null;
			if (this.Covers(x, y, zoomLevel))
			{
				result = base.GetUri(x, y, zoomLevel);
			}
			return result;
		}
		private bool Covers(int x, int y, int zoomLevel)
		{
			bool flag = true;
			if (this.ZoomRange != null)
			{
				flag = ((double)zoomLevel >= this.ZoomRange.From && (double)zoomLevel <= this.ZoomRange.To);
			}
			if (flag && this.BoundingRectangle != null)
			{
				flag = this.BoundingRectangle.Intersects(LocationRectTileSource.GetBoundingRectangle(x, y, zoomLevel));
			}
			return flag;
		}
		private static LocationRect GetBoundingRectangle(int x, int y, int zoomLevel)
		{
			double num = 1.0 / Math.Pow(2.0, (double)zoomLevel);
			Rect rect = new Rect((double)x * num, (double)y * num, num, num);
			return LocationRect.CreateLocationRect(new GeoCoordinate[]
			{
				MercatorUtility.LogicalPointToLocation(new Point(rect.get_Left(), rect.get_Top())),
				MercatorUtility.LogicalPointToLocation(new Point(rect.get_Right(), rect.get_Bottom()))
			});
		}
	}
}
